// <copyright file="MenuRepository.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Cms.Data.Entities;

    public class MenuRepository : CmsGenericRepository<CmsMenuType>, IMenuRepository
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="menu"></param>
        public MenuRepository(CmsDataContext ctx, IMenuItemRepository menu) : base(ctx)
        {
            service = menu;   
        }

        #region IMenuData Members
        private readonly IMenuItemRepository service;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        public CmsMenu LoadMenu(string menuName)
        {
            // MenuItemData service = new MenuItemData();
            // TODO: Rework
            var cms = service.LoadAll(filter: x => (x.MenuType.Type == menuName && x.ParentId == null)).ToList()
                          .Select(x => 
                              new CmsMenu() { 
                                  Id = x.Id, 
                                  Type= x.MenuType.Type, 
                                  Description = x.MenuType.Description, 
                                  CreatedAt = x.CreatedAt, 
                                  Title = x.Title,
                                  MenuItems = service.LoadAll(y => y.MenuType.Type == menuName && x.Visible == true, null, "").OrderBy(z => z.SortOrder).ToList() 
                              })
                              .FirstOrDefault();

            return cms;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsMenu Load(int id) 
        {
            var ent = base.Load(id);
          //  MenuItemData service = new MenuItemData();
            return new CmsMenu() { Id = ent.Id, 
                Title = ent.Title, 
                CreatedAt = ent.CreatedAt, 
                Description = ent.Description, 
                Type = ent.Type, 
                MenuItems = service.LoadAll(y => y.MenuType.Type == ent.Type).ToList() };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenu> LoadAll()
        {
            return base.LoadAll().Select(x => new CmsMenu() { Id = x.Id, Type = x.Type, Description = x.Description, CreatedAt = x.CreatedAt, Title = x.Title });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsMenuItem LoadMenuItem(int id)
        {
            return service.Load(id);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenuItem> LoadAllItems()
        {
            return service.LoadAll();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void SaveMenuItem(CmsMenuItem item)
        {
            service.Save(item, item.Id == 0);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="menu"></param>
        public void Save(CmsMenu menu)
        {
            
        //base.Save(menu);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newTitle"></param>
        public void Rename(int id, string newTitle)
        {
            var type = base.LoadAll().FirstOrDefault(x => x.Id == id);
            if (type != null)
            {
                type.Description = newTitle;
            }
            base.Save(type, false);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            base.Delete(id);
            base.Save();
        }

        #endregion
    }
}